package com.longtu.chat.controller;

import com.longtu.chat.ws.MessageWebSocket;
import com.longtu.chat.vo.JSONResult;
import com.longtu.chat.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@CrossOrigin
@Slf4j
public class UserController {
    @Autowired
    MessageWebSocket messageWebSocket;

    @GetMapping("getUsersName")
    public JSONResult getAllUsersName() {
        return JSONResult.success(MessageWebSocket.getUsersName());
    }

    @GetMapping("getUser/{name}")
    public JSONResult getUser(@PathVariable String name) {
        return JSONResult.success(MessageWebSocket.getUserByName(name));
    }

    @PutMapping("updateUserState/{name}")
    public JSONResult updateUserState(@PathVariable String name, @RequestParam Integer state) {
        Set<UserVo> userVoList = MessageWebSocket.getUserVoList();
        for (UserVo userVo : userVoList) {
            if (userVo.getLoginName().equals(name)) {
                userVo.setState(state);
                break;
            }
        }
        log.info("改变用户状态 name: {} , state: {}", name, state);
        return JSONResult.success();
    }

    @PutMapping("updateDisMax/{max}")
    public JSONResult updateDisMax(@PathVariable Integer max) {
        MessageWebSocket.distanceMax = max;
        return JSONResult.success();
    }

    @GetMapping("getDisMax")
    public JSONResult getDisMax() {
        return JSONResult.success(MessageWebSocket.distanceMax);
    }

    @GetMapping("getUsers")
    public JSONResult getUsers() {
        return JSONResult.success(MessageWebSocket.getUserVoList());
    }
}
